package com.weetop.aklyscan.activity;

import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.qmuiteam.qmui.layout.QMUIButton;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.appConfig.MyApplication;
import com.weetop.aklyscan.base.BaseFragment;
import com.weetop.aklyscan.base.BaseUrl;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.bean.ChaXunBean;
import com.weetop.aklyscan.bean.FittingDetailBean;
import com.weetop.aklyscan.bean.FittingInfo;
import com.weetop.aklyscan.bean.InMatterItemBean;
import com.weetop.aklyscan.bean.InStockItemBean;
import com.weetop.aklyscan.bean.IntoProductSaveBean;
import com.weetop.aklyscan.bean.ProductDetailBean;
import com.weetop.aklyscan.bean.ProductInfo;
import com.weetop.aklyscan.bean.ScanShelfCodeBean;
import com.weetop.aklyscan.bean.TypeInfo;
import com.weetop.aklyscan.bean.ZXGoodsInfo;
import com.weetop.aklyscan.bean.ZXOrderInfo;
import com.weetop.aklyscan.presenter.WxInStockPresent;
import com.weetop.aklyscan.utils.BaseUtils;
import com.weetop.aklyscan.utils.widget.MyDialogUtil;
import com.weetop.aklyscan.view.WxInStockView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cc.shinichi.library.tool.utility.ui.ToastUtil;

public class Fragment_in_wx extends BaseFragment<WxInStockPresent> implements WxInStockView, CommonRecyclerAdapter.OnItemClickListener {
    private static final int HUOJIA_CODE = 12000;
    private static final int REQUEST_HUOJIA_CODE_SCAN_ONE = 22000;

    @BindView(R.id.rcy_data)
    RecyclerView rcyData;
    @BindView(R.id.rcy_data2)
    RecyclerView rcyData2;
    @BindView(R.id.rcy_data3)
    RecyclerView rcyData3;
    Unbinder unbinder;
    //    @BindView(R.id.order_spinner)
//    TextView orderSpinner;
    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.tv_order_code)
    TextView tvOrderCode;
    @BindView(R.id.tv_stock_name)
    EditText tvStockName;
    @BindView(R.id.tv_stock_postion)
    EditText tvStockPostion;
    @BindView(R.id.tv_totle_postion)
    TextView tvTotlePostion;
    @BindView(R.id.peijian_name)
    TextView peijianNameView;

    @BindView(R.id.btn_into_save)
    QMUIButton btnIntoSave;
    @BindView(R.id.chanpin_ll)
    LinearLayout chanpin_ll;
    @BindView(R.id.peijian_ll)
    LinearLayout peijian_ll;
    @BindView(R.id.peijian_ll1)
    LinearLayout peijian_ll1;
    @BindView(R.id.peijian_sn)
    TextView peijian_sn;
    @BindView(R.id.xinghao)
    TextView xinghao;
    @BindView(R.id.mingcheng)
    TextView mingcheng;
    @BindView(R.id.order_type_spinner)
    TextView order_type_spinner;
    @BindView(R.id.huojia)
    TextView huojia;
    @BindView(R.id.qingkong)
    TextView qingkong;
    @BindView(R.id.zhongzhuanxiang_ll)
    LinearLayout zhongzhuanxiang_ll;
    @BindView(R.id.zhongzhuanxiang)
    TextView zhongzhuanxiang;
    @BindView(R.id.qingkong1)
    TextView qingkong1;
    @BindView(R.id.button1)
    TextView button1;
    @BindView(R.id.button)
    TextView button;
    @BindView(R.id.record2)
    TextView record2;
    @BindView(R.id.th_type_ll)
    View typeView;
    @BindView(R.id.tv_stock_goods_name)
    View stockNumView;
    @BindView(R.id.tv_stock_goods_name_line)
    View stockNumLineView;
    @BindView(R.id.peijian_no)
    EditText peijian_no;
    @BindView(R.id.peijian_num)
    EditText peijian_num;
    @BindView(R.id.leixing)
    LinearLayout leixing;
    @BindView(R.id.huojia_ll)
    LinearLayout huojia_ll;
    @BindView(R.id.tv_zhongzhuan)
    TextView tv_zhongzhuan;
    @BindView(R.id.iv_zhongzhuan)
    ImageView iv_zhongzhuan;
    @BindView(R.id.iv_zhongzhuan_1)
    ImageView iv_zhongzhuan_1;
    @BindView(R.id.tv_zhongzhuan_1)
    TextView tv_zhongzhuan_1;

    private CommonRecyclerAdapter<ZXOrderInfo> adapter1;
    private CommonRecyclerAdapter<FittingDetailBean> adapter12;
    private CommonRecyclerAdapter<InStockItemBean> adapter2;
    private CommonRecyclerAdapter<InMatterItemBean> adapter3;
    // 产品类型：1产品；2配件
    private int inStockType = 1;
    // 扫码类型：1编号；2货架；3产品
    private int scanType = 1;
    private String orderCode;
    private List<ZXOrderInfo> productISList;
    private List<FittingDetailBean> fittingDetailBeanList;
    private ScanShelfCodeBean scanShelfCodeBean;
    private List<InStockItemBean> list = new ArrayList<>();
    private List<InMatterItemBean> list1 = new ArrayList<>();
    private String barcode;
    private HashMap<String, Integer> fittingMap;

    String huoCode;
    String xiangCode, orderID;
    int isDan = 1, isChanpin = 1;

    boolean bSave = true;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                peijian_no.setText((String) msg.obj);
            }
        }
    };

    @Override
    protected WxInStockPresent createPresenter() {
        return new WxInStockPresent(this, mActivity);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_in_wx, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        rcyData2.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter2 = new CommonRecyclerAdapter<InStockItemBean>(mActivity, R.layout.item_in_wx, list) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, InStockItemBean item, int position) {
                helper.setText(R.id.tv_xuhao, position + 1 + "");
                helper.setText(R.id.tv_huopin_sn, item.getGoods_code());
                helper.setText(R.id.tv_huopin_xinghao, item.getGoods_model());
                helper.setText(R.id.tv_huopin_mingcheng, item.getGoods_name());
                helper.setText(R.id.tv_xiangnei_huojia, huojia.getText().toString().trim());
                if (item.isError()) {
                    helper.setBackgroundRes(R.id.ll, R.drawable.tab_line_error);
                } else {
                    helper.setBackgroundRes(R.id.ll, R.drawable.tab_line);
                }
                helper.setOnClickListener(R.id.btn_delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        System.out.println(list.size() + ":" + position);
                        if (list.size() > position) {
                            adapter2.remove(item);
                            list.remove(item);
                            adapter2.notifyDataSetChanged();
                        }
                    }
                });
            }
        };
        rcyData2.setAdapter(adapter2);

        rcyData3.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter3 = new CommonRecyclerAdapter<InMatterItemBean>(mActivity, R.layout.item_in_wx_peijian2, list1) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, InMatterItemBean item, int position) {
                helper.setText(R.id.tv_xuhao, position + 1 + "");
                helper.setText(R.id.tv_huopin_sn, item.getMatter_code());
                helper.setText(R.id.tv_huopin_xinghao, item.getMatter_name());
                helper.setText(R.id.tv_huopin_mingcheng, item.getHuojia());
                helper.setText(R.id.tv_xiangnei_huojia, item.getCount() + "");
                helper.setText(R.id.tv_zhongzhuan, item.getWarehouse_coordinates());
                if (MyApplication.getPort().equals("1")) {
                    helper.setVisible(R.id.tv_huopin_mingcheng, true);
                    helper.setVisible(R.id.tv_zhongzhuan, isDan == 0);
                }
                helper.setOnClickListener(R.id.btn_delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (list1.size() > position) {
                            adapter3.remove(item);
                            list1.remove(item);
                            adapter3.notifyDataSetChanged();
                        }
                    }
                });
            }
        };
        rcyData3.setAdapter(adapter3);

        if (MyApplication.getPort().equals("2")) {
            button.setVisibility(View.GONE);
            record2.setVisibility(View.VISIBLE);
            order_type_spinner.setText("配件采购");
            isChanpin = 0;
            chanpin_ll.setVisibility(View.GONE);
            peijian_ll.setVisibility(View.VISIBLE);
            xinghao.setText("配件编号");
            mingcheng.setText("配件名称");
            if (isDan == 0) {
                peijian_ll1.setVisibility(View.GONE);
            } else {
                peijian_ll1.setVisibility(View.VISIBLE);
            }
            typeView.setVisibility(View.GONE);
            stockNumView.setVisibility(View.GONE);
            stockNumLineView.setVisibility(View.GONE);
            leixing.setVisibility(View.GONE);
            huojia_ll.setVisibility(View.GONE);
            zhongzhuanxiang_ll.setVisibility(View.GONE);
        } else {
            button.setVisibility(View.VISIBLE);
            record2.setVisibility(View.GONE);
        }
//        mPresenter.Into10();
        mPresenter.Into11();
//        onBarcodeEvent("PJCG202108230006");
//        orderSpinner.setTag("1");
    }

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {
    }

    @Override
    public void getInStockDetail01Success(BaseModel<List<ProductDetailBean>> o) {
    }

    @Override
    public void getInStockDetail02Success(BaseModel<List<FittingDetailBean>> o) {
//        orderSpinner.setEnabled(false);
        fittingDetailBeanList = o.getData();
        if (fittingDetailBeanList != null && fittingDetailBeanList.size() > 0) {
            rcyData.setLayoutManager(new LinearLayoutManager(mActivity));
            adapter12 = new CommonRecyclerAdapter<FittingDetailBean>(mActivity, R.layout.item_scanout1, fittingDetailBeanList) {
                @Override
                public void onUpdate(BaseAdapterHelper helper, FittingDetailBean item, int position) {
                    helper.setText(R.id.tv_xuhao, position + 1 + "");
                    helper.setText(R.id.tv_huopin_xinghao, item.getMatter_code());
                    helper.setText(R.id.tv_huopin_mingcheng, item.getMatter_name());
                    helper.setText(R.id.tv_yingru_zongliang, item.getNumber() + "");
//                    helper.setText(R.id.tv_huopin_dindan, "配件");
                    EditText weiru = helper.getView(R.id.et_weiruzongliang);
                }
            };
            rcyData.setAdapter(adapter12);
        }
        if (list.size() > 0) {
            list.clear();
            adapter2.clear();
            adapter3.clear();
        }
        if (fittingDetailBeanList.size() > 0) {
            playRing("shujuyouxiao.mp3");
        } else {
            playRing("shujuwuxiao.mp3");
        }
    }

    @Override
    public void scanShelfCodeSuccess(BaseModel<ScanShelfCodeBean> o) {
        scanShelfCodeBean = o.getData();
        tvStockName.setText(scanShelfCodeBean.getArea_name());
        tvStockPostion.setText(scanShelfCodeBean.getArea_code());
//        tvTotlePostion.setText(scanShelfCodeBean);

        if (list.size() > 0) {
            list.clear();
            adapter2.clear();
            adapter3.clear();
        }
    }

    @Override
    public void getInto07Success(String matter_code, BaseModel<FittingInfo> o) {
        FittingInfo data = o.getData();
        InMatterItemBean matterItems = new InMatterItemBean();
        matterItems.setMatter_code(data.getMatter_code());
        matterItems.setMatter_name(data.getMatter_name());
        matterItems.setHuojia(huojia.getText().toString().trim());
        matterItems.setCount(Integer.valueOf(peijian_num.getText().toString().trim()));

        peijianNameView.setText(data.getMatter_name());
        list1.add(matterItems);
        adapter3.add(matterItems);
        rcyData3.smoothScrollToPosition(list1.size());

    }

    @Override
    public void getProductInfSuccess(BaseModel<ProductInfo> o) {
//        ProductInfo data = o.getData();
//        InStockItemBean goodsItems = new InStockItemBean();
//        goodsItems.setGoods_code(data.getGoods_sn());
//        goodsItems.setGoods_name(data.getGoods_name());
//        //产品类型：1产品；2配件
//        goodsItems.setGoods_type(1);
//        goodsItems.setCount(1);
//        //状态:-1已作废；0未入库；1在库里；2未出库；3已出库
//        goodsItems.setStatus(0);
//        list.add(goodsItems);
//        adapter2.add(goodsItems);
//        adapter3.add(goodsItems);
    }

    @Override
    public void saveIntoKuSuccess(BaseModel<Object> o) {
//        orderSpinner.setEnabled(true);
        ToastUtils.showShort("入库保存成功");
    }

    @Override
    public void getInto08Success(BaseModel<ZXGoodsInfo> o) {
        ZXGoodsInfo data = o.getData();
        InStockItemBean goodsItems = new InStockItemBean();
        goodsItems.setGoods_code(data.getSerial_code());
        goodsItems.setGoods_name(data.getGoods_name());
        goodsItems.setGoods_model(data.getGoods_model());
        goodsItems.setAudio_url(data.getAudio_url());
        goodsItems.setCount(1);
        //状态:-1已作废；0未入库；1在库里；2未出库；3已出库
        goodsItems.setStatus(data.getStatus());
        if (data.getStatus() == 1) {
            ToastUtil.getInstance()._short(getActivity(), "产品已入库");
//            playUrl(goodsItems.getAudio_url(), 0);
            playUrl("", 0);
//            playRing("wuxiao.mp3");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getGoods_code().equals(goodsItems.getGoods_code())) {
                MyDialogUtil.showDialog(getActivity(), "数据无效已删除");
//                playUrl(goodsItems.getAudio_url(), 0);
                playUrl("", 0);
//                playRing("wuxiao.mp3");
                return;
            }
        }
        for (int i = 0; i < productISList.size(); i++) {
            if (goodsItems.getGoods_model().equals(productISList.get(i).getGoods_model())) {
                list.add(goodsItems);
                adapter2.add(goodsItems);
                rcyData2.smoothScrollToPosition(list.size());
                playUrl(goodsItems.getAudio_url(), 1);
//                playRing("luruchenggong.mp3");
                return;
            }
        }
        MyDialogUtil.showDialog(getActivity(), "数据无效已删除");
//        playUrl(goodsItems.getAudio_url(), 0);
        playUrl("", 0);
//        playRing("wuxiao.mp3");
    }

    @Override
    public void getInto10Success(BaseModel<List<TypeInfo>> o) {
//        List<TypeInfo> data = o.getData();
//        into10Item = new String[data.size()];
//        for (int i = 0; i < data.size(); i++) {
//            into10Item[i] = data.get(i).getName();
//        }
    }

    @Override
    public void getInto12Success(BaseModel<List<ZXOrderInfo>> o, final int b) {
        productISList = o.getData();
        int p = 0;
        for (int i = 0; i < productISList.size(); i++) {
            p = p + productISList.get(i).getCount();
        }
        tvOrderCode.setText(orderID);
        tvTotlePostion.setText(p + "");
        if (productISList != null && productISList.size() > 0) {
            rcyData.setLayoutManager(new LinearLayoutManager(mActivity));
            adapter1 = new CommonRecyclerAdapter<ZXOrderInfo>(mActivity, R.layout.item_scanout1, productISList) {
                @Override
                public void onUpdate(BaseAdapterHelper helper, ZXOrderInfo item, int position) {
                    helper.setText(R.id.tv_xuhao, position + 1 + "");
                    helper.setText(R.id.tv_huopin_xinghao, item.getGoods_model());
                    helper.setText(R.id.tv_huopin_mingcheng, item.getGoods_name());
                    helper.setText(R.id.tv_yingru_zongliang, item.getStored() + "");
                    helper.setText(R.id.et_weiruzongliang, item.getNot_stored() + "");
                    helper.setText(R.id.tv_huopin_dindan, item.getCount() + "");
                }
            };
            adapter1.setOnItemClickListener(this);
            rcyData.setAdapter(adapter1);
            if (b == 1) {
                playRing("shujuyouxiao.mp3");
            }
        } else {
            adapter1.clear();
            adapter1.notifyDataSetChanged();
            if (b == 1) {
                playRing("shujuwuxiao.mp3");
            }
        }
        tvOrderCode.setBackgroundResource(R.drawable.et_grey);
        if (TextUtils.isEmpty(huojia.getText().toString().trim())) {
            huojia.setBackgroundResource(R.drawable.et_yellow);
            zhongzhuanxiang.setBackgroundResource(R.drawable.et_grey);
            peijian_no.setBackgroundResource(R.drawable.et_grey);
        } else {
            huojia.setBackgroundResource(R.drawable.et_grey);
            if (TextUtils.isEmpty(zhongzhuanxiang.getText().toString().trim())) {
                zhongzhuanxiang.setBackgroundResource(R.drawable.et_yellow);
            } else {
                zhongzhuanxiang.setBackgroundResource(R.drawable.et_grey);
            }
            if (TextUtils.isEmpty(peijian_no.getText().toString().trim())) {
                peijian_no.setBackgroundResource(R.drawable.et_yellow);
            } else {
                peijian_no.setBackgroundResource(R.drawable.et_grey);
            }
        }
    }

    @Override
    public void getInto13Success(BaseModel<Object> o) {
        huojia.setText(huoCode);
        huojia.setBackgroundResource(R.drawable.et_grey);
        if (TextUtils.isEmpty(zhongzhuanxiang.getText().toString().trim())) {
            zhongzhuanxiang.setBackgroundResource(R.drawable.et_yellow);
        }
        if (TextUtils.isEmpty(peijian_no.getText().toString().trim())) {
            peijian_no.setBackgroundResource(R.drawable.et_yellow);
        }
        playRing("shujuyouxiao.mp3");
    }

    @Override
    public void getInto14Success(BaseModel<List<ZXGoodsInfo>> o) {
        List<ZXGoodsInfo> data = o.getData();
        if (isChanpin == 1) {
            zhongzhuanxiang.setText(xiangCode);
            zhongzhuanxiang.setBackgroundResource(R.drawable.et_grey);
            List<InStockItemBean> list14 = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                InStockItemBean goodsItems = new InStockItemBean();
                goodsItems.setGoods_code(data.get(i).getSerial_code());
                goodsItems.setGoods_name(data.get(i).getGoods_name());
                goodsItems.setGoods_model(data.get(i).getGoods_model());
                goodsItems.setBarcode(data.get(i).getBarcode());
                goodsItems.setWarehouse_coordinates(zhongzhuanxiang.getText().toString().trim());
                goodsItems.setCount(1);
                //状态:-1已作废；0未入库；1在库里；2未出库；3已出库
                goodsItems.setStatus(data.get(i).getStatus());
                list14.add(goodsItems);
            }
            if (list14.size() < 1) {
                playRing("shujuwuxiao.mp3");
                return;
            }
            if (list.stream().filter(w -> String.valueOf(w.getBarcode()).equals(list14.get(0).getBarcode())).findAny().isPresent()) {
                playRing("shujuwuxiao.mp3");
                return;
            }
            list.addAll(list14);
            adapter2.addAll(list14);
            playRing("luruchenggong.mp3");
        } else {
            List<InMatterItemBean> list14 = new ArrayList<>();
            zhongzhuanxiang.setText(xiangCode);
            zhongzhuanxiang.setBackgroundResource(R.drawable.et_grey);
            for (int i = 0; i < data.size(); i++) {
                InMatterItemBean goodsItems = new InMatterItemBean();
                goodsItems.setMatter_code(data.get(i).getSerial_code());
                goodsItems.setMatter_name(data.get(i).getGoods_name());
                goodsItems.setMatter_model(data.get(i).getGoods_model());
                goodsItems.setBarcode(data.get(i).getBarcode());
                goodsItems.setHuojia(huojia.getText().toString().trim());
                goodsItems.setCount(data.get(i).getNum());
                goodsItems.setWarehouse_coordinates(zhongzhuanxiang.getText().toString().trim());
                //状态:-1已作废；0未入库；1在库里；2未出库；3已出库
                goodsItems.setStatus(data.get(i).getStatus());
                list14.add(goodsItems);
            }
            if (list14.size() < 1) {
                playRing("shujuwuxiao.mp3");
                return;
            }
            if (list1.stream().filter(w -> String.valueOf(w.getBarcode()).equals(list14.get(0).getBarcode())).findAny().isPresent()) {
                playRing("shujuwuxiao.mp3");
                return;
            }
            list1.addAll(list14);
            adapter3.addAll(list14);
            playRing("luruchenggong.mp3");
        }
    }

    @Override
    public void getInto15Success(BaseModel<String> o) {
        bSave = true;
        String data = o.getData();
        if (!TextUtils.isEmpty(data)) {
//            ToastUtil.getInstance()._short(getActivity(), "订单" + data + "入库失败");
            List<String> errorList = Arrays.asList(data.split(","));
            for (int j = 0; j < list.size(); j++) {
                list.get(j).setError(false);
                for (int i = 0; i < errorList.size(); i++) {
                    if (errorList.get(i).equals(list.get(j).getGoods_code())) {
                        list.get(j).setError(true);
                    }
                }
            }
            adapter2.notifyDataSetChanged();
        } else {
            ToastUtil.getInstance()._short(getActivity(), "操作成功");
            List<ModelBean> modelList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                final int a = i;
                if (!modelList.stream().filter(w -> String.valueOf(w.getModel()).equals(list.get(a).getGoods_model())).findAny().isPresent()) {
                    ModelBean modelBean = new ModelBean();
                    modelBean.setModel(list.get(i).getGoods_model());
                    modelBean.setNum(0);
                    modelList.add(modelBean);
                }
            }
            for (int i = 0; i < modelList.size(); i++) {
                for (int j = 0; j < list.size(); j++) {
                    if (modelList.get(i).getModel().equals(list.get(j).getGoods_model())) {
                        modelList.get(i).setNum(modelList.get(i).getNum() + 1);
                    }
                }
            }
            try {
                JSONArray jsonArray = new JSONArray();
                JSONObject tmpObj = null;
                for (int i = 0; i < modelList.size(); i++) {
                    tmpObj = new JSONObject();
                    tmpObj.put("goods_model", modelList.get(i).getModel());
                    tmpObj.put("new_num", modelList.get(i).getNum());
                    jsonArray.put(tmpObj);
                    tmpObj = null;
                }
                mPresenter.Into24(tvOrderCode.getText().toString(), jsonArray);
            } catch (Exception e) {
                e.printStackTrace();
            }

            list.clear();
            list1.clear();
            adapter1.clear();
            adapter1.notifyDataSetChanged();
            adapter2.clear();
            adapter2.notifyDataSetChanged();
            adapter3.clear();
            adapter3.notifyDataSetChanged();
            huojia.setText("");
            huojia.setBackgroundResource(R.drawable.et_grey);
            zhongzhuanxiang.setText("");
            zhongzhuanxiang.setBackgroundResource(R.drawable.et_grey);
            playRing("rukuwancheng.mp3");
            if (isChanpin == 1) {
                mPresenter.Into12("1", "1", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 0);
            } else {
                if (MyApplication.getPort().equals("1")) {
                    mPresenter.Into12("2", "3", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 0);
                } else {
                    mPresenter.Into12("2", "4", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 0);
                }
            }

        }
    }

    @Override
    public void getInto16Success(BaseModel<String> o) {
        bSave = true;
        String data = o.getData();
        if (!TextUtils.isEmpty(data)) {
            ToastUtil.getInstance()._short(getActivity(), "配件" + data + "入库失败");
        } else {
            ToastUtil.getInstance()._short(getActivity(), "操作成功");
            list.clear();
            list1.clear();
            adapter1.clear();
            adapter1.notifyDataSetChanged();
            adapter2.clear();
            adapter2.notifyDataSetChanged();
            adapter3.clear();
            adapter3.notifyDataSetChanged();
            huojia.setText("");
            huojia.setBackgroundResource(R.drawable.et_grey);
            zhongzhuanxiang.setText("");
            peijian_no.setText("");
            peijian_num.setText("");
            zhongzhuanxiang.setBackgroundResource(R.drawable.et_grey);
            tvTotlePostion.setText("");
            playRing("rukuwancheng.mp3");
            if (MyApplication.getPort().equals("1")) {
                mPresenter.Into12("2", "3", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 1);
            } else {
                mPresenter.Into12("2", "4", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 1);
            }
        }
    }

    @Override
    public void getInto17Success(BaseModel<String> o) {
        bSave = true;
        String data = o.getData();
        if (!TextUtils.isEmpty(data)) {
            ToastUtil.getInstance()._short(getActivity(), "配件" + data + "入库失败");
        } else {
            ToastUtil.getInstance()._short(getActivity(), "操作成功");
            list.clear();
            list1.clear();
            adapter1.clear();
            adapter1.notifyDataSetChanged();
            adapter2.clear();
            adapter2.notifyDataSetChanged();
            adapter3.clear();
            adapter3.notifyDataSetChanged();
            huojia.setText("");
            huojia.setBackgroundResource(R.drawable.et_grey);
            zhongzhuanxiang.setText("");
            peijian_no.setText("");
            peijian_num.setText("");
            peijianNameView.setText("");
            zhongzhuanxiang.setBackgroundResource(R.drawable.et_grey);
            tvTotlePostion.setText("");
            playRing("rukuwancheng.mp3");
            if (MyApplication.getPort().equals("1")) {
                mPresenter.Into12("2", "3", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 1);
            } else {
                mPresenter.Into12("2", "4", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 1);
            }
        }
    }

    @Override
    public void getInto24Success(BaseModel<List<ChaXunBean>> o) {
        List<ChaXunBean> chaXunBeans = o.getData();
        for (int i = 0; i < chaXunBeans.size(); i++) {
            TiJiaoDialog tiJiaoDialog = new TiJiaoDialog(getActivity(), chaXunBeans.get(i), chaXunBeans.size(), chaXunBeans.size() - i, 1);
            tiJiaoDialog.show();
        }
    }

    @Override
    public void getInto25Success(BaseModel<Object> o) {
        tvOrderCode.setText(orderID);
        tvOrderCode.setBackgroundResource(R.drawable.et_grey);
        if (isChanpin == 1) {
            mPresenter.Into12("1", "1", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 1);
        } else {
            mPresenter.Into12("2", "3", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 1);
        }
    }

    @Override

    public void onDataError() {
        playRing("shujuwuxiao.mp3");
    }

    @Override
    public void onError() {
        MyDialogUtil.showDialog(getActivity(), "数据无效已删除");
        playRing("wuxiao.mp3");
    }

    @Override
    public void onUpFail(String mag) {
        bSave = true;
        MyDialogUtil.showDialog(getActivity(), mag);
        playRing("wuxiao.mp3");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btn_into_save, R.id.tv_order_code, R.id.type, R.id.order_type_spinner, R.id.button, R.id.qingkong, R.id.qingkong1,
            R.id.button1, R.id.record, R.id.record2, R.id.qingkong2, R.id.qingkong3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_order_code:
                break;
            case R.id.type:
                XPopup.get(getActivity()).asCenterList("请选择类型", new String[]{"单件", "多件"},
                        // null, /** 图标Id数组，可无 **/
                        // 1,    /** 选中的position，默认没有选中效果 **/
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                list.clear();
                                adapter2.clear();
                                adapter2.notifyDataSetChanged();
                                list1.clear();
                                adapter3.clear();
                                adapter3.notifyDataSetChanged();
                                huojia.setText("");
                                huojia.setBackgroundResource(R.drawable.et_grey);
                                zhongzhuanxiang.setText("");
                                zhongzhuanxiang.setBackgroundResource(R.drawable.et_grey);
                                peijian_no.setText("");
                                peijian_num.setText("");
                                if (position == 0) {
                                    type.setText("单件");
                                    peijian_sn.setText("SN号");
                                    zhongzhuanxiang_ll.setVisibility(View.GONE);
                                    isDan = 1;
                                    tv_zhongzhuan.setVisibility(View.GONE);
                                    iv_zhongzhuan.setVisibility(View.GONE);
                                    tv_zhongzhuan_1.setVisibility(View.GONE);
                                    iv_zhongzhuan_1.setVisibility(View.GONE);
                                    if (isChanpin == 0) {
                                        peijian_ll1.setVisibility(View.VISIBLE);
                                    }
                                } else if (position == 1) {
                                    type.setText("多件");
                                    peijian_sn.setText("中转箱编号");
                                    zhongzhuanxiang_ll.setVisibility(View.VISIBLE);
                                    isDan = 0;
                                    tv_zhongzhuan.setVisibility(View.VISIBLE);
                                    iv_zhongzhuan.setVisibility(View.VISIBLE);
                                    tv_zhongzhuan_1.setVisibility(View.VISIBLE);
                                    iv_zhongzhuan_1.setVisibility(View.VISIBLE);
                                    if (isChanpin == 0) {
                                        peijian_ll1.setVisibility(View.GONE);
                                    }
//                                    onBarcodeEvent("PPS-PJ-B-1-1");
//                                    handler.postDelayed(new Runnable() {
//                                        @Override
//                                        public void run() {
//                                            onBarcodeEvent("123456");
//                                        }
//                                    }, 2000);

                                }
                            }
                        })
                        .show();
                break;
            case R.id.order_type_spinner:
                if (MyApplication.getPort().equals("1")) {
                    XPopup.get(getActivity()).asCenterList("请选择凭证类型", new String[]{"产品采购", "配件采购"}, new OnSelectListener() {
                        @Override
                        public void onSelect(int position, String text) {
                            if (adapter1 != null) {
                                adapter1.clear();
                                adapter1.notifyDataSetChanged();
                            }
                            tvTotlePostion.setText("");
                            order_type_spinner.setText(text);

                            if (position == 0) {
                                isChanpin = 1;
                                chanpin_ll.setVisibility(View.VISIBLE);
                                peijian_ll.setVisibility(View.GONE);
                                peijian_ll1.setVisibility(View.GONE);
                                xinghao.setText("产品型号");
                                mingcheng.setText("产品名称");
                            } else {
                                isChanpin = 0;
                                chanpin_ll.setVisibility(View.GONE);
                                peijian_ll.setVisibility(View.VISIBLE);
                                xinghao.setText("配件型号");
                                mingcheng.setText("配件名称");
                                if (isDan == 0) {
                                    peijian_ll1.setVisibility(View.GONE);
                                } else {
                                    peijian_ll1.setVisibility(View.VISIBLE);
                                }
                            }
//                            onBarcodeEvent("PJCG202112200001");
//                            handler.postDelayed(new Runnable() {
//                                @Override
//                                public void run() {
//                                    onBarcodeEvent("PPS-PJ-B-1-1");
//                                }
//                            }, 2000);
                        }
                    })
                            .show();
                } else {
                    XPopup.get(getActivity()).asCenterList("请选择凭证类型", new String[]{"配件采购"}, new OnSelectListener() {
                        @Override
                        public void onSelect(int position, String text) {
                            if (adapter1 != null) {
                                adapter1.clear();
                                adapter1.notifyDataSetChanged();
                            }
                            tvTotlePostion.setText("");
                            order_type_spinner.setText(text);

                            isChanpin = 0;
                            chanpin_ll.setVisibility(View.GONE);
                            peijian_ll.setVisibility(View.VISIBLE);
                            xinghao.setText("配件型号");
                            mingcheng.setText("配件名称");
                            if (isDan == 0) {
                                peijian_ll1.setVisibility(View.GONE);
                            } else {
                                peijian_ll1.setVisibility(View.VISIBLE);
                            }
                        }
                    })
                            .show();
                }
                break;
            case R.id.btn_into_save:
                try {
                    if (BaseUtils.fastClick()) {
                        if (!bSave) {
                            return;
                        }
                        if (TextUtils.isEmpty(tvOrderCode.getText().toString())) {
                            ToastUtil.getInstance()._short(getActivity(), "入库凭证单编号不能为空");
                            return;
                        }
                        if (TextUtils.isEmpty(order_type_spinner.getText().toString())) {
                            ToastUtil.getInstance()._short(getActivity(), "入库凭证类型不能为空");
                            return;
                        }
                        bSave = false;
                        if (isChanpin == 1) {//产品
                            List<IntoProductSaveBean> items = new ArrayList<>();
                            JSONArray jsonArray = new JSONArray();
                            JSONObject tmpObj = null;
                            for (int i = 0; i < list.size(); i++) {
                                tmpObj = new JSONObject();
                                tmpObj.put("serial_code", list.get(i).getGoods_code());
                                tmpObj.put("transit_code", TextUtils.isEmpty(list.get(i).getBarcode()) ? "" : list.get(i).getBarcode());
                                jsonArray.put(tmpObj);
                                tmpObj = null;
                            }
                            if (jsonArray.length() > 0) {
                                String itemsString = GsonUtils.toJson(items);
                                mPresenter.Into15("1", tvOrderCode.getText().toString().trim(), huojia.getText().toString().trim(),
                                        jsonArray, "1");
                            } else {
                                bSave = true;
                                ToastUtils.showShort("无新增产品");
                            }
                        } else {// 配件
                            JSONArray jsonArray = new JSONArray();
                            JSONObject tmpObj = null;
                            if (list1.size() < 1) {
                                ToastUtils.showShort("无新增配件");
                                return;
                            }
                            if (MyApplication.getPort().equals("1")) {
                                for (int i = 0; i < list1.size(); i++) {
                                    tmpObj = new JSONObject();
                                    tmpObj.put("matter_code", list1.get(i).getMatter_code());
                                    tmpObj.put("transit_code", list1.get(i).getBarcode());
                                    tmpObj.put("num", list1.get(i).getCount());
                                    tmpObj.put("tier_code", list1.get(i).getHuojia());
                                    jsonArray.put(tmpObj);
                                    tmpObj = null;
                                }
                                mPresenter.Into16(tvOrderCode.getText().toString().trim(), jsonArray);
                            } else {
                                for (int i = 0; i < list1.size(); i++) {
                                    tmpObj = new JSONObject();
                                    tmpObj.put("matter_code", list1.get(i).getMatter_code());
                                    tmpObj.put("num", list1.get(i).getCount());
                                    tmpObj.put("tier_code", "");
                                    tmpObj.put("transit_code", "");
                                    jsonArray.put(tmpObj);
                                    tmpObj = null;
                                }
                                mPresenter.Into17(tvOrderCode.getText().toString().trim(), jsonArray);
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button:
                if (TextUtils.isEmpty(tvOrderCode.getText().toString())) {
                    ToastUtil.getInstance()._short(getActivity(), "入库凭证单编号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(order_type_spinner.getText().toString())) {
                    ToastUtil.getInstance()._short(getActivity(), "入库凭证类型不能为空");
                    return;
                }
                if (isChanpin == 1) {
                    mPresenter.Into12("1", "1", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 1);
                } else {
                    if (MyApplication.getPort().equals("1")) {
                        mPresenter.Into12("2", "3", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 1);
                    } else {
                        mPresenter.Into12("2", "4", MyApplication.getPort(), tvOrderCode.getText().toString().trim(), 1);
                    }
                }
                break;
            case R.id.qingkong:
                huojia.setText("");
                playRing("qingkong.mp3");
                if (!TextUtils.isEmpty(tvOrderCode.getText().toString().trim())) {
                    huojia.setBackgroundResource(R.drawable.et_yellow);
                } else {
                    huojia.setBackgroundResource(R.drawable.et_grey);
                }
                zhongzhuanxiang.setBackgroundResource(R.drawable.et_grey);
                peijian_no.setBackgroundResource(R.drawable.et_grey);
                break;
            case R.id.qingkong1:
                zhongzhuanxiang.setText("");
                playRing("qingkong.mp3");
                if (!TextUtils.isEmpty(huojia.getText().toString()) && !TextUtils.isEmpty(tvOrderCode.getText().toString().trim())) {
                    zhongzhuanxiang.setBackgroundResource(R.drawable.et_yellow);
                }
                break;
            case R.id.qingkong2:
                peijian_no.setText("");
                playRing("qingkong.mp3");
                if (!TextUtils.isEmpty(huojia.getText().toString()) && !TextUtils.isEmpty(tvOrderCode.getText().toString().trim())) {
                    peijian_no.setBackgroundResource(R.drawable.et_yellow);
                }
                break;
            case R.id.qingkong3:
                tvOrderCode.setText("");
                tvOrderCode.setBackgroundResource(R.drawable.et_yellow);
                huojia.setBackgroundResource(R.drawable.et_grey);
                zhongzhuanxiang.setBackgroundResource(R.drawable.et_grey);
                peijian_no.setBackgroundResource(R.drawable.et_grey);
                list.clear();
                list1.clear();
                if (adapter1 != null) {
                    adapter1.clear();
                    adapter1.notifyDataSetChanged();
                }
                if (adapter2 != null) {
                    adapter2.clear();
                    adapter2.notifyDataSetChanged();
                }
                if (adapter3 != null) {
                    adapter3.clear();
                    adapter3.notifyDataSetChanged();
                }

                huojia.setText("");
                huojia.setBackgroundResource(R.drawable.et_grey);
                zhongzhuanxiang.setText("");
                peijian_no.setText("");
                peijian_num.setText("");
                zhongzhuanxiang.setBackgroundResource(R.drawable.et_grey);
                tvTotlePostion.setText("");
                playRing("qingkong.mp3");
                break;
            case R.id.button1:
                if (TextUtils.isEmpty(peijian_no.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(getActivity(), "配件编号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(peijian_num.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(getActivity(), "配件数量不能为空");
                    return;
                }
                if (MyApplication.getPort().equals("1")) {
                    if (TextUtils.isEmpty(huojia.getText().toString().trim())) {
                        ToastUtil.getInstance()._short(getActivity(), "货架号不能为空");
                        return;
                    }
                }
                mPresenter.Into07(peijian_no.getText().toString().trim());
                break;
            case R.id.record2:
            case R.id.record:
                if (TextUtils.isEmpty(tvOrderCode.getText().toString())) {
                    ToastUtil.getInstance()._short(getActivity(), "入库凭证单编号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(order_type_spinner.getText().toString())) {
                    ToastUtil.getInstance()._short(getActivity(), "入库凭证类型不能为空");
                    return;
                }
                Intent intent = new Intent(getActivity(), InRecordListActivity.class);
                if (isChanpin == 1) {
                    intent.putExtra("order_type", "1");
                } else {
                    if (MyApplication.getPort().equals("1")) {
                        intent.putExtra("order_type", "3");
                    } else {
                        intent.putExtra("order_type", "4");
                    }
                }
                intent.putExtra("order_sn", tvOrderCode.getText().toString().trim());
                getActivity().startActivity(intent);
                break;
        }
    }

    public void onBarcodeEvent(String code) {
        System.out.println("扫描结果2：" + code);
        if (isChanpin == 1) {
            if (TextUtils.isEmpty(tvOrderCode.getText().toString().trim())) {
                if (TextUtils.isEmpty(order_type_spinner.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(getActivity(), "入库凭证类型不能为空");
                    return;
                }
                orderID = code;
                mPresenter.Into25("1", code);
            } else if (TextUtils.isEmpty(huojia.getText().toString().trim())) {
                huoCode = code;
                mPresenter.Into13(code);
            } else if (isDan == 0 && TextUtils.isEmpty(zhongzhuanxiang.getText().toString().trim())) {
                xiangCode = code;
                mPresenter.Into14(code);
            } else if (isDan == 1) {
                if (TextUtils.isEmpty(tvOrderCode.getText().toString())) {
                    ToastUtil.getInstance()._short(getActivity(), "入库凭证单编号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(order_type_spinner.getText().toString())) {
                    ToastUtil.getInstance()._short(getActivity(), "入库凭证类型不能为空");
                    return;
                }
                mPresenter.Into08("1", code, tvOrderCode.getText().toString().trim());
            }
        } else {
            if (TextUtils.isEmpty(tvOrderCode.getText().toString().trim())) {
                if (TextUtils.isEmpty(order_type_spinner.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(getActivity(), "入库凭证类型不能为空");
                    return;
                }
                orderID = code;
                if (MyApplication.getPort().equals("1")) {
                    mPresenter.Into12("2", "3", MyApplication.getPort(), code, 1);
                } else {
                    mPresenter.Into12("2", "4", MyApplication.getPort(), code, 1);
                }
//                tvOrderCode.setText(code);
//                playRing("shujuyouxiao.mp3");
            } else if (MyApplication.getPort().equals("2")) {
                Message message = new Message();
                message.what = 1;
                message.obj = code;
                handler.sendMessage(message);
            } else if (TextUtils.isEmpty(huojia.getText().toString().trim())) {
                huoCode = code;
                mPresenter.Into13(code);
            } else if (isDan == 0 && TextUtils.isEmpty(zhongzhuanxiang.getText().toString().trim())) {
                if (TextUtils.isEmpty(huojia.getText().toString())) {
                    ToastUtil.getInstance()._short(getActivity(), "货架号不能为空");
                    return;
                }
                xiangCode = code;
                mPresenter.Into14(code);
            } else if (isDan == 1) {
                Message message = new Message();
                message.what = 1;
                message.obj = code;
                handler.sendMessage(message);
            }
        }
    }

    private AssetManager assetManager;
    private MediaPlayer player = new MediaPlayer();
    private boolean isPlay;

    private void playRing(String fileName) {
        try {
            isPlay = false;
            assetManager = getActivity().getAssets();
            AssetFileDescriptor fileDescriptor = assetManager.openFd(fileName);
//            if (player.isPlaying()) {
            player.reset();
//            }
            player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(),
                    fileDescriptor.getLength());
            player.prepare();
            player.start();
            player.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                @Override
                public void onSeekComplete(MediaPlayer mp) {
                    player.stop();
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void playUrl(String fileName, final int i) {
        if (TextUtils.isEmpty(fileName) || fileName.equals("null")) {
            if (i == 1) {
                playRing("luruchenggong.mp3");
                return;
            } else {
                playRing("wuxiao.mp3");
                return;
            }
        }
        try {
            isPlay = true;
            player.reset();
            if (!fileName.contains("http")) {
                player.setDataSource(BaseUrl.mp3Url + fileName);
            } else {
                player.setDataSource(fileName);
            }
            player.prepare();
            player.start();
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    if (isPlay) {
                        if (i == 1) {
                            playRing("luruchenggong.mp3");
                        } else {
                            playRing("wuxiao.mp3");
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPause() {
        if (player.isPlaying()) {
            player.stop();
        }
        super.onPause();
    }

    @Override
    public void onDestroy() {
        player.release();
        super.onDestroy();
    }

    class ModelBean {
        String model;
        int num;

        public String getModel() {
            return model;
        }

        public int getNum() {
            return num;
        }

        public void setModel(String model) {
            this.model = model;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }
}
